/**
 * Created by HUANGFR2 on 8/19/2016.
 */
const mongoose = require('mongoose');
var Dish = mongoose.model("Dish");
var moment = require('moment');
var request = require('request');

exports.findAll = function(req, res){
    Dish.find({}, function (err, dishs) {
        if(err){
            console.error('find all dish error : ' + err);
            res.json({
                error : 'find all dish error : ' + err
            });
        } else {
            var dIds = [];
            dishs.forEach(function (dish) {
                dIds.push(dish._id);
            });
            var formData = {
                dids : JSON.stringify(dIds)
            };
            request.post({
                url: 'http://zha-ita113-w7:3038/dishWebService/queryDish',
                formData: formData
            }, function optionalCallback(err, httpResponse, body) {
                if (err) {
                    console.error('request dish error : ' + err);
                    res.json({
                        error : 'request dish error : ' + err
                    });
                    return;
                }
                try {
                    var dishs_ = JSON.parse(body);
                    dishs.forEach(function (dish) {
                        dish.detail = dishs_[dish._id];
                    });
                    res.json({
                        msg : 'SUCCESS',
                        dishs : dishs
                    })
                } catch (err){
                    res.json({
                        error : 'request dish error : Server Error'
                    });
                }

            });
        }
    })
};

exports.updateDishStatus = function(req, res){
    var did = req.body.did;
    var status = req.body.status;
    Dish.findOne({_id : did}, function (err, dish) {
        if(err){
            console.error('find dish status error :' + err);
            res.json({
                error : 'find dish status error :' + err
            });
        } else {
            dish.status = status;
            if(status == 1){
                dish.expiredDate = moment().add(7, 'days');
            }
            if(status == 2){
                dish.expiredDate = moment();
            }
            dish.save(function (err) {
                if(err){
                    console.error('update dish status error :' + err);
                    res.json({
                        error : 'update dish status error :' + err
                    });
                } else {
                    res.json({
                        msg : 'SUCCESS',
                        dish : dish
                    })
                }
            })
        }
    })
};

exports.dishRecommendation = function (req, res) {
    Dish.find({status : 1, expiredDate : { $gt: moment() }}, {_id : 1}, function (err, dishs) {
        if(err){
            console.error('get recommended dish error : ' + err);
            res.json({
                error : 'get recommended dish error : ' + err
            })
        } else {
            var dishs_ = [];
            dishs.forEach(function (dish) {
                dishs_.push(dish._id)
            });
            res.json(dishs_);
        }
    })
};